package com.bsming.code;

import java.io.FileInputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

import org.yaml.snakeyaml.Yaml;

/**
 * @author magenm
 * @date 2018/7/17 9:48
 */
public class GeneratorTest {

    public static void main(String[] args) throws Exception {

        Map proj = getProjInfo();
        Map ds = getDataSource();

        PomFile pomFile = new PomFile();
        String[] models = proj.get("models").toString().split(",");
        for (String model : models) {
            new GeneratorCode(
                    proj.get("pkgName").toString(),
                    model,
                    proj.get("modelPrefix").toString(),
                    ds.get("url").toString(),
                    ds.get("username").toString(),
                    ds.get("password").toString(),
                    ds.get("tbPrefix").toString(),
                    proj.get("author").toString()
            ).generator();

            String modelNameX = null;
            if ("service".equals(model)) {
                modelNameX = "common";
            }
            if ("web".equals(model)) {
                modelNameX = "service";
            }
            pomFile.genPom(
                    proj.get("name").toString(),
                    proj.get("pkgName").toString(),
                    proj.get("modelPrefix").toString(),
                    model,
                    modelNameX);
        }
        pomFile.genRootPom(proj.get("modelPrefix").toString(), models);
    }

    private static Map getProjInfo() throws Exception {
        //初始化Yaml解析器
        Yaml yaml = new Yaml();
        Path yml = Paths.get("src/main/resources/application.yml");
        Map map = (Map) yaml.load(new FileInputStream(yml.toFile()));
        Map projMap = (Map) map.get("project");
        return projMap;
    }

    private static Map getDataSource() throws Exception {
        //初始化Yaml解析器
        Yaml yaml = new Yaml();
        Path devYml = Paths.get("src/main/resources/application-dev.yml");
        Map devMap = (Map) yaml.load(new FileInputStream(devYml.toFile()));
        Map dsMap = (Map) devMap.get("spring");
        return (Map)dsMap.get("datasource");
    }

}
